<template>
	<div>
		<el-dialog title="定时发布稿件" v-model="isShowDialog" width="30%">
			<el-form ref="selectTimerFormRef" :model="formData" :rules="formRules" label-width="120px">
				<el-row :gutter="35">
					<el-col>
						<el-form-item label="定时发布时间" prop="timerTime">
							<el-date-picker v-model="formData.timerTime" value-format="YYYY-MM-DD HH:mm:ss" type="datetime" placeholder="请选定时发布时间" class="w100"/>
						</el-form-item>
					</el-col>
					<el-col>
						<p class="color-warning">温馨提示：定时发布时间必须大于当前时间，定时发布时间只精确到分时刻，秒时刻忽略不计。</p>
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="onCancel">取 消</el-button>
					<el-button type="primary" @click="onSubmit">确 定</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script lang="ts">
import { reactive, toRefs, ref } from 'vue';
import { ElMessage } from 'element-plus';
import { timingDocument } from '/@/api/cms/document/index';
export default {
	name: 'cms/timer/select',
	setup(props, context) {
		const selectTimerFormRef = ref();
		const state = reactive({
			isShowDialog: false,
			document : {},
			formData: {
				timerTime: '',
			},
			formRules : {
				timerTime: { required: true, message: '定时发布时间不能为空', trigger: 'change' },
			}
		});

		// 打开弹窗
		const openDialog = (row?: object) => {
			state.isShowDialog = true;
			state.document = Object.assign({}, row);
		};
		// 关闭弹窗
		const closeDialog = () => {
			state.isShowDialog = false;
			for(var key in state.formData) {
				state.formData[key] = undefined;
			}
			selectTimerFormRef.value.resetFields();
		};
		// 取消
		const onCancel = () => {
			closeDialog();
		};
		// 新增
		const onSubmit = () => {
			selectTimerFormRef.value.validate(valid => {
				if(valid){
					let params = {
						ids: [state.document.id],
						timerTime: state.formData.timerTime
					}
					timingDocument(params).then((res) => {
						if(res.code === 0){
							ElMessage.success('定时发布成功');
							context.emit('submit');
							closeDialog();
						}
					});
				}
			});
		};
		
		return {
			selectTimerFormRef,
			openDialog,
			closeDialog,
			onCancel,
			onSubmit,
			...toRefs(state),
		};
	},
};
</script>

<style>
</style>
